<?php

namespace App\Http\Middleware;

use App\Models\UserVisit;
use App\Services\VisitService;
use Closure;

class VisitSign
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $_visit_service=new VisitService();
        $info=UserVisit::where('sign',$_visit_service->get_visit())->first();
        if(!isset($info->id))
        {
            $id=$_visit_service->add_visit();
            $info=UserVisit::find($id)->first();
            session(['user_name' =>$info->user_name]);
        }else{
            session(['user_name' =>$info->user_name]);
        }
        return $next($request);
    }
}
